<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="getFormData"){
        $request_id = $_POST['request_id'];
        $requestProcess_id = $_POST['requestProcess_id'];
        $sequence_approver = $_POST['sequenceApprover'];
        $getFormValues = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbform f on r.form_id = f.id where r.id = '$request_id'","row");
        $getDataArray = explode("~",$getFormValues['data']);
        if($_POST['btnProperties']=="none"){
            $buttons = "";
        }else{
            $arraySplit = substr($_POST['btnProperties'],0,strlen($_POST['btnProperties'])-1);
        
            $arraySplit = explode("+",$arraySplit);
            for($a=0;$a<count($arraySplit);$a++){
                    $buttonID =  explode(":",$arraySplit[$a]);
                    $nextApprover = explode(">",$buttonID[1]);
                    $requestStatus = $nextApprover[1];
                    $buttonID = $buttonID[0];
                    $nextApprover = $nextApprover[0];
                    $getButton = $db->query("SELECT * FROM tbbuttons where id = $buttonID","row");
                    $buttons.="<button class='button-icon approverButton pointer ' style='font-size:10px;margin-right:5px;' id='button_".$request_id."_".$requestProcess_id."' object-id='".$nextApprover."' request_status='".$requestStatus."' sequence-approver='$sequence_approver'>".$getButton['button']."</button>";
            }   
        }
        echo "<div class='pull-right pointer closeModal'><li class='icon-remove'></li></div><br />";
        echo form_extract($getFormValues['form_title'],$getFormValues['description'],$getFormValues['label_placement']);
        ?>
        <table width=400>
        <?php
        for($c=0;$c<count($getDataArray)-1;$c++){
            $dataLabel = explode("->",$getDataArray[$c]);
            $getLabel = $db->query("SELECT * FROM tbfield_output where id = '$dataLabel[0]'","row");
            $comma = "";
            ?>
            <div class="fields">
                <div class="label fontGray"><?php echo setColon($getLabel['f_label']); ?></div>
                <div class="input_position2 fontGray" style="margin-top: 10px">
                    <?php echo setFormValues($dataLabel[1]); ?>
                </div>
            </div>
            <?php
        }
        echo "<div class='pull-left' style='margin-top:15px;width:40%'>";
        echo setRequestFlow($getFormValues['requestFlowObj_id'],$getFormValues['requestFlowApprover'],$requestProcess_id,$company_id);
        echo $buttons;
        echo "</div>";
        ?>
            
        </table>
        <div class='pull-left' style='margin-top:15px;width:40%'>
            <span style="font-size:11px;font-weight:bold">Comments</span>
            <?php
            echo '
            <div style="float: left;background-color: whiteSmoke;padding: 5px;width: 345px;" id="addComment_'.$request_id.'_3">
                    ';
                    // Function to get your additional comment
                    echo addComment($request_id,$request_id,3,"LIMIT 4"," ");
                    echo '<div class="appendNewComment" id="appendNewComment_'.$request_id.'_3"></div>';
            echo '
                    <div style="float: left;width: 10%;">';
                    //  Function to get your avatar
                        echo myAvatar($auth['extension'],$user_id,"30","30","none");
                        
            echo '                   
                    </div>
                    <div style="float: left;width: 85%;padding-left: 5px;">
                        <textarea class="input-comment" id="newComment_'.$request_id.'_3" style="height:30px;width:300px;"></textarea><br />
                        <input type="button" class="button-icon addButtonComment" data-comment-type="3" value="Comment" data-id="'.$request_id.'">
                    </div>
            </div>';
            ?>
        </div>
        <?php
    }else if($_POST['action']=="editForm"){
        $request_id = $_POST['request_id'];
        $getFormValues = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbform f on r.form_id = f.id where r.id = '$request_id'","row");
        $getDataArray = explode("~",$getFormValues['data']);
        echo "<div class='pull-right pointer closeModal'><li class='icon-remove'></li></div><br />";
        echo form_extract($getFormValues['form_title'],$getFormValues['description'],$getFormValues['label_placement']);
        ?>
        <table width=400>
        <?php
        for($c=0;$c<count($getDataArray)-1;$c++){
            $dataLabel = explode("->",$getDataArray[$c]);
            $getLabel = $db->query("SELECT * FROM tbfield_output where id = '".$dataLabel[0]."'","row");
            $comma = "";
            ?>
            <div class="fields">
                <div class="label fontGray"><?php echo setColon($getLabel['f_label']).getRequired($getLabel['id']);; ?></div>
                <div class="input_position2 fontGray" id="" style="margin-top: 10px">
                    <?php
                    echo $getLabel['f_type']($getLabel['id'],$dataLabel[1],$getLabel['f_size'],$getFormValues['form_id'],$company_id,$getLabel['options']);
                    ?>
                </div>
            </div>
            <?php
        }
        ?>
        </table>
        <br />
        <label style="font-size: 10px;"><input type="checkbox" id="editSendRequest">Send to Requestor</label><br /><br />
        <input type="button" class="button-icon pointer editFormUser" rel='save' id="editFormUser_<?php echo $request_id;?>" value="Save">
        <br />
        <div class='pull-left' style='margin-top:15px;width:40%'>
            <span style="font-size:11px;font-weight:bold">Comments</span>
            <?php
            echo '
            <div style="float: left;background-color: whiteSmoke;padding: 5px;width: 345px;" id="addComment_'.$request_id.'_3">
                    ';
                    // Function to get your additional comment
                    echo addComment($request_id,$request_id,2,"LIMIT 4"," ");
                    echo '<div class="appendNewComment" id="appendNewComment_'.$request_id.'_3"></div>';
            echo '
                    <div style="float: left;width: 10%;">';
                    //  Function to get your avatar
                        echo myAvatar($auth['extension'],$user_id,"30","30","none");
                        
            echo '                   
                    </div>
                    <div style="float: left;width: 85%;padding-left: 5px;">
                        <textarea class="input-comment" id="newComment_'.$request_id.'_3" style="height:30px;width:300px;"></textarea><br />
                        <input type="button" class="button-icon addButtonComment" data-comment-type="3" value="Comment" data-id="'.$request_id.'">
                    </div>
            </div>';
            ?>
        </div>
        <?php
    }
}
    
?>